package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-a-value-of-a-mysterious-function-closest-to-target/description/https://leetcode.cn/problems/find-a-value-of-a-mysterious-function-closest-to-target/description/
 * @create 2025/10/7 11:01
 * @since JDK17
 */

public class Solution06 {
    public int closestToTarget(int[] arr, int target) {
        int n = arr.length, ans = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {   //
            int x = arr[i];
            for (int j = i - 1; j >= 0 && (x & arr[j]) != arr[j]; j--) {
                arr[j] &= x;
                ans = Math.min(ans, Math.abs(arr[j] - target));
            }
            ans = Math.min(ans, Math.abs(x - target));
        }

        return ans;
    }
}
